package com.yunxiangyue

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.yunxiangyue.app.R

class comment_adapter(val commentList: ArrayList<com.yunxiangyue.Com>): RecyclerView.Adapter<comment_adapter.ViewHolder>() {

    var count=0
    open var selected=-1
    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
         val name=view.findViewById<TextView>(R.id.Name)
         val date=view.findViewById<TextView>(R.id.date)
         val comments=view.findViewById<TextView>(R.id.Comments)
         val userPhoto=view.findViewById<ImageView>(R.id.userPhoto)
        val thumbsUp=view.findViewById<Button>(R.id.thumbsUp)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view= LayoutInflater.from(parent.context).inflate(R.layout.comment_item,parent,false)
        val viewHolder=ViewHolder(view)
//        viewHolder.itemView.setOnClickListener(){
//            var position=viewHolder.absoluteAdapterPosition
//        }
        viewHolder.thumbsUp.setOnClickListener{
            if(count%2==0){
                it.setBackgroundResource(R.drawable.ic_icon)
            }else{
                it.setBackgroundResource(R.drawable.ic_dianzan)
            }
            count++
        }
        view.isLongClickable=true
        return ViewHolder(view)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val comment=commentList[position]
//        holder.num.text=music.num
//        holder.music_name.text=music.musicName
//        holder.music_play.text=music.play+"—"+music.album

        holder.userPhoto.setImageResource(comment.bitmap)
        holder.name.text=comment.name
        holder.date.text=comment.date
        holder.comments.text=comment.comments

        holder.also {
            it.itemView.setOnLongClickListener {
                selected=holder.adapterPosition
                false
            }

//            it.itemView.setOnClickListener {
//                if (mOnItemClickListener != null) {
//                    mOnItemClickListener!!.onItemClick(view, position)
//                }
//            }
        }
//        holder.itemView.setOnClickListener { view ->
//            if (mOnItemClickListener != null) {
//                mOnItemClickListener!!.onItemClick(view, position)
//            }
//        }
//        holder.itemView.setOnLongClickListener {
//            selected=holder.adapterPosition
//            false
//        }

    }

    override fun getItemCount(): Int {
        return commentList.size
    }


    //    在适配器中定义点击元素的接口和包含的方法，点击事件和长点击事件
    interface OnItemClickListener {
        fun onItemClick(view: View?, position: Int)
        fun onItemLongClick(view: View?, position: Int)
    }

    private var mOnItemClickListener: OnItemClickListener? = null

    fun setOnItemClickListener(onItemClickListener: OnItemClickListener?) {
        mOnItemClickListener = onItemClickListener
    }


//    添加语句到recycylerView首部
//    public fun addItem(position: Int,bitmap:Int,name:String,date:String,comment:String):Boolean{
//        if(position<commentList.size&&position>=0){
//            commentList.add(Com(bitmap,name,date,comment))
//            notifyItemInserted(1)
//            return true
//        }
//    return false
//    }

}